﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;

namespace YFIN.Service.Crawlers
{
    public class Spider
    {
        string rooturl1 = "http://www.pbc.gov.cn/publish/goutongjiaoliu/524/index.html";
        private AnalyzeHtml analyze;
        private IList<News> UrlList = new List<News>();


        public Spider(AnalyzeHtml analyze)
        {
            this.analyze = analyze;
        }

        public String GetResponse(string url)
        {
            try
            {
                WebRequest requset = WebRequest.Create(url);
                WebResponse response = requset.GetResponse();

                Stream s = response.GetResponseStream();
                StreamReader sr = new StreamReader(s, System.Text.Encoding.UTF8);
                string html = sr.ReadToEnd();
                s.Close();
                sr.Close();
                return html;
            }
            catch (Exception)
            {
                return String.Empty;
            }

        }



        public void Init()
        {
            analyze.GetUrlListByparseString(new News(new Uri(rooturl1)), GetResponse(rooturl1));

        }

        public IList<News> start()
        {
            List<News> res = new List<News>();
            bool flag = false;
            if (UrlList.Count == 0)
            {
                if (analyze.UrlList.Count > 0)
                {
                    res.AddRange(UrlList = ListCopy(analyze.UrlList));
                    analyze.UrlList.Clear();
                }
                else
                {
                    flag = true;
                }
            }

            if (UrlList.Count > 0)
            {
                for (int i = 0; i < UrlList.Count; i++)
                {
                    if (UrlList[i].CurrentDepth > 1)
                    {
                        UrlList.RemoveAt(i);
                    }
                    else
                    {
                        res.AddRange(analyze.GetUrlListByparseString(UrlList[i], GetResponse(UrlList[i].UrlLink.ToString())));
                        UrlList.RemoveAt(i);
                    }
                }
            }
            if (!flag)
            {
                res.AddRange(start());
            }
            return res;
        }


        private IList<News> ListCopy(IList<News> url)
        {
            IList<News> list = new List<News>();
            for (int i = 0; i < url.Count; i++)
            {
                list.Add(url[i]);
            }
            return list;
        }
    }
}